import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useComposeStore = defineStore('compose', () => {
    // 状态
    const selectedHumans = ref([])
    const selectedBackgrounds = ref([])
    const globalParams = ref({
        canvas_width: 1080,
        canvas_height: 1920,
        video_scale: 0.6,
        video_x: 0,
        video_y: 400,
        bg_scale: 1.0,
        bg_x: 0,
        bg_y: 0,
        keep_human_ratio: true,
        keep_bg_ratio: false,
        output_format: 'mp4'
    })

    // 组合列表
    const combinations = ref([])

    // 计算属性
    const hasCombinations = computed(() => combinations.value.length > 0)

    // 方法
    const addHuman = (human) => {
        if (!selectedHumans.value.find(h => h.file_id === human.file_id)) {
            selectedHumans.value.push(human)
        }
    }

    const removeHuman = (fileId) => {
        const index = selectedHumans.value.findIndex(h => h.file_id === fileId)
        if (index > -1) {
            selectedHumans.value.splice(index, 1)
        }
    }

    const addBackground = (background) => {
        if (!selectedBackgrounds.value.find(b => b.file_id === background.file_id)) {
            selectedBackgrounds.value.push(background)
        }
    }

    const removeBackground = (fileId) => {
        const index = selectedBackgrounds.value.findIndex(b => b.file_id === fileId)
        if (index > -1) {
            selectedBackgrounds.value.splice(index, 1)
        }
    }

    const clearSelections = () => {
        selectedHumans.value = []
        selectedBackgrounds.value = []
    }

    const generateCombinations = () => {
        combinations.value = []
        selectedHumans.value.forEach((human, hIndex) => {
            selectedBackgrounds.value.forEach((bg, bIndex) => {
                combinations.value.push({
                    id: `${human.file_id}_${bg.file_id}`,
                    human,
                    background: bg,
                    params: { ...globalParams.value },
                    output_name: `${human.original_name.split('.')[0]}_${bg.original_name.split('.')[0]}.${globalParams.value.output_format}`
                })
            })
        })
    }

    const updateCombinationParam = (combinationId, paramKey, value) => {
        const combination = combinations.value.find(c => c.id === combinationId)
        if (combination) {
            combination.params[paramKey] = value
        }
    }

    const removeCombination = (combinationId) => {
        const index = combinations.value.findIndex(c => c.id === combinationId)
        if (index > -1) {
            combinations.value.splice(index, 1)
        }
    }

    const updateGlobalParams = (params) => {
        globalParams.value = { ...globalParams.value, ...params }
    }

    const applyGlobalParamsToAll = () => {
        combinations.value.forEach(combination => {
            combination.params = { ...globalParams.value }
        })
    }

    return {
        selectedHumans,
        selectedBackgrounds,
        globalParams,
        combinations,
        hasCombinations,
        addHuman,
        removeHuman,
        addBackground,
        removeBackground,
        clearSelections,
        generateCombinations,
        updateCombinationParam,
        removeCombination,
        updateGlobalParams,
        applyGlobalParamsToAll
    }
})